﻿@{
    ViewBag.Title = "流向收发存统计明细表";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<link href="~/Content/css/container_report_print.css" rel="stylesheet" media="print" />
<style type="text/css">
    .print {
        display: none;
    }

    .tool_c {
        padding: 10px 0;
    }

    .btn-group {
        padding: 0 10px 0 0;
    }



    .btn-group:last-child a.btn:first-child {
        margin: 0 0.5em 0 0 !important;
    }

    .input_inline input {
        display: inline-block;
        width: 4em;
    }

    .btn-group input[type=text] {
        height: 2em !important;
    }
</style>
<script>
    $(function () {
        Index.init();
    });

    var Index = {
        defaultDate:@Html.Raw(ViewData["LedgerDate"].ToString()),
        printTemplate: null,
        title: "{1}流向收发存统计明细表",
        curUnit: '@ViewData["curUnit"].ToString()',
        curUserName: '@ViewData["curUserName"].ToString()',
        curDate: '@ViewData["curDate"].ToString()',
        curTime: '@ViewData["curTime"].ToString()',
        FHappenCompanyNO: '@ViewData["FHappenCompanyNO"].ToString()',
        FContainerNO: '@ViewData["FContainerNO"].ToString()',
        FContainerName: '@ViewData["FContainerName"].ToString()',
        FHappenCompanyNOName: '@ViewData["FHappenCompanyNOName"].ToString()',
        FAccountDate:'@ViewData["FAccountDate"].ToString()',
        dataList: null,
        printItemTemplate: "",
        init: function () {
            with (Index) {
                printItemTemplate = $("#print1 .page").html();

                initControl();

                loadDataList();
            }
        },
        initControl: function () {
            var cols = "FAccountDate,FHappenCompanyNO,FContainerNO,FContainerName,FHappenCompanyNOName".split(",");

            cols.forEach(function(col){
                $("#{0}1".format(col)).val(Index[col]);
            });

            //批次查询 btn_search1
            $("#btn_search1").on("click", function () {
                var $gridList1 = $("#gridList1");

                var conn1 = {};

                $(".submitForm").toArray().forEach(function(item){
                    var id = $(this).attr("id").subStr(0,$(this).attr("id").lastIndexOf("1"));

                    conn1[id] = $(this).val();

                });

                $gridList1.jqGrid('setGridParam', {
                    postData: conn1,
                }).trigger('reloadGrid');
            });
            //打印
            $("#butnPrint1").on("click", function () {
                $("#print1").jqprint();
            });
            //导出
            $("#butnExport1").on("click", function () {
                var param = { id: "V_RQK_32_LX_StoConAIAOAccD", param: Index.buildExportConn("FContainerNO,FHappenCompanyNO,FAccountDate".split(","), "1") };
                
                var url = "/Common/ExcelExportById?{0}".format($.param(param));

                location.href = url;
            });
            //返回
            $("#butnReturn").on("click", function () {
                history.back(-1);
            });
        },
        buildExportConn: function (params, num) {
            var conn = "";

            !num && (num = "");

            params.forEach(function (item) {
                var val = $("#{0}{1}".format(item, num)).val();

                !!val && (conn += ("and {0} = '{1}'".format(item, val)));
            });

            return conn;
        },
        loadDataList: function () {
            var conn1 = {};

            $(".submitForm").toArray().forEach(function(item){
                var id = $(item).attr("id").substr(0,$(item).attr("id").lastIndexOf("1"));

                conn1[id] = $(item).val();
            });


            var $gridList1 = $("#gridList1");///BOM/Blankingcard/GetBlankingcards
          //  SELECT FDocuNO,FDate,FHappComNO as FHappenCompanyNO,FHappComName as FHappenCompanyName, FContainerNO, FContainerName, FQuantity_RK, FQuantity_FX, FQuantity_CK, FQuantity_BS, FQuantity_BR, FQuantity_BC, FQuantity_PC,FStoreSetNO,FQuantity_QC,case FContainerType when '0' then '空箱' when '1' then '重箱' when '2' then '--' end as ConType FROM V_RQK_32_LX_StoConAIAOAcc WHERE UseUnit ='使用单位' and FAccountDate ='台账日期' AND FContainerNO ='上一页选择的容器编号' AND FHappenCompanyNO ='上一页选择的仓库单位' ORDER BY FDate asc,FCreateDate asc,FCreateTime asc
            $gridList1.dataGridForJson({
                url: "/WareHouse/Container/GetMoveToReceiveAndDispatchStatisticsDetails",
                postData: conn1,
                height: $(window).height() - 240,
                colModel: [
                { label: '日期', name: 'FDate', width: 100, align: 'center', sortable: false },
                { label: '单据编号', name: 'FDocuNO', width: 140, align: 'left', sortable: false },
                { label: '发生单位', name: 'FHappenCompanyNO', width: 80, align: 'center', sortable: false, hidden: true },
                { label: '发生单位名称', name: 'FHappenCompanyName', width: 140, align: 'left', sortable: false },
                { label: '摘要', name: 'ConType', width: 80, align: 'left', sortable: false },
                { label: 'ColFQuantity_QCS(隐藏)', name: 'FQuantity_QC', width: 120, align: 'right', sortable: false, hidden: true  },
                { label: '期初数量', name: 'QC', width: 80, align: 'right', sortable: false },
                { label: '入库数量', name: 'FQuantity_RK', width: 80, align: 'right', sortable: false },
                { label: '出库数量', name: 'FQuantity_CK', width: 80, align: 'right', sortable: false },
                { label: '废品数量', name: 'FQuantity_BS', width: 60, align: 'right', sortable: false },
                { label: '返修数量', name: 'FQuantity_FX', width: 60, align: 'right', sortable: false },
                { label: '盈亏数量', name: 'FQuantity_PC', width: 60, align: 'right', sortable: false, },
                { label: '库存数量', name: 'JC', width: 60, align: 'right', sortable: false, },
                { label: '仓库定置', name: 'FStoreSetNO', width: 100, align: 'center', sortable: false },
                 { label: '', name: '', width: $(window).width() - 1100, align: 'left'}
                ],
                pager: "#gridPager1",
                sortname: 'FDate,FCreateDate,FCreateTime',
                sortorder: "asc,asc,asc",
                viewrecords: true,
                rownumWidth: 50,
                onSelectRow: function (rowId) {

                },
                loadComplete: function (data) {
                    Index.dataList = data;

                    var columnInfo = [
                              { caption: '仓库单位', name: 'FHappenCompanyNO'},
                              { caption: '仓库单位名称', name: 'FHappenCompanyName'},
                              { caption: '容器编号', name: 'FContainerNO'},
                              { caption: '容器名称', name: 'FContainerName'},
                              { caption: '重箱<br/>库存', name: 'ZXKC'},
                              { caption: '空箱<br/>库存', name: 'KXKC'},
                              { caption: '库存<br/>数量', name: 'KC'},
                              { caption: '期初<br/>数量', name: 'FQuantity_QC'},
                              { caption: '入库<br/>数量', name: 'FQuantity_RK'},
                              { caption: '出库<br/>数量', name: 'FQuantity_CK'},
                              { caption: '废品<br/>数量', name: 'FQuantity_BS'},
                              { caption: '返修<br/>数量', name: 'FQuantity_FX'},
                              { caption: '盈亏<br/>数量', name: 'FQuantity_PC'}
                    ];

                    var title = Index.title.format(new Date().format("yyyy年MM月"))

                    Index.renderPrintData($("#print1"), data.rows, columnInfo, title);
                }
            });

            $gridList1.jqGrid('setFrozenColumns');

            $gridList1.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        },
        renderPrintData: function (c, data, colunmInfo, title) {
            c.empty();

            var rows = 16;

            var max = data.length;

            var item = { page: 1, maxPage: 1, title: title, unit: Index.curUnit, user: Index.curUserName, date: Index.curDate, time: Index.curTime, table: null };

            var maxPage = Math.ceil(max / rows);

            item.maxPage = maxPage;

            for (var i = 0; i < maxPage; i++) {
                var isFirst = true;

                var start = i * rows, end = (i + 1) * rows;

                end > max && (end = max);

                var table = $("<table>"), pageItem = $("<div>").addClass("page");

                item.page = i + 1;

                for (var j = start; j < end; j++) {
                    var cur = data[j];

                    isFirst && (function () {
                        var title = $("<tr>");

                        for (var idx in colunmInfo) {
                            $("<th>").html(colunmInfo[idx].caption).appendTo(title);
                        }

                        title.appendTo(table);

                        isFirst = false;
                    })();

                    !isFirst && (function () {
                        var row = $("<tr>");

                        for (var idx in colunmInfo) {
                            $("<td>").text(cur[colunmInfo[idx].name]).appendTo(row);
                        }

                        row.appendTo(table);
                    })();
                }

                item.table = "<table>{0}</table>".format(table.html());

                pageItem.html(Index.printItemTemplate.format(item)).appendTo(c);
            }
        },
        buildSql: function (pars, suffix, colInfo, tableName) {
            var param = { sql: "" }, conn = "", cols = [];

            !suffix && (suffix = "");

            param.sql = "SELECT {2} FROM {3} where useunit = '{1}' {0}";

            pars.forEach(function (item) {
                var val = $("#{0}{1}".format(item, suffix)).val();

                !!val && (conn += " and {0} = '{1}'".format(item, val));
            });

            colInfo.forEach(function (item) {
                cols.push("{name} {caption}".format(item));
            });

            param.sql = param.sql.format(conn, Index.curUnit, cols.join(","), tableName);

            console.log(param.sql);

            return param;
        }
    };
</script>


<div class="layout" id="layout" style="width: 100%;">
    <div class="tool_c">
        <div class="btn-group input-width-185">
            &nbsp;容器编号<br />
            <input id="FContainerNO1" name="" placeholder="" type="text" class="submitForm form-control required" />
            <input id="FHappenCompanyNO1" name="" placeholder="" type="hidden" class="submitForm form-control required" />
            <input id="FAccountDate1" name="" placeholder="" type="hidden" class="submitForm form-control required" />
        </div>
        <div class="btn-group input-width-185">
            &nbsp;容器名称<br />
            <input id="FContainerName1" name="" placeholder="" type="text" class="form-control required" />
        </div>
        <div class="btn-group input-width-185">
            &nbsp;仓库单位<br />
            <input id="FHappenCompanyNOName1" name="" placeholder="" type="text" class="form-control required" />
        </div>
        <div class="btn-group">
            <a id="btn_search1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-search"></span>查询</a>
            <a class="btn btn-primary center-block" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span>&nbsp刷新</a>
            <a id="butnPrint1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-print"></span>&nbsp打印</a>
            <a id="butnExport1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-download"></span>&nbsp导出</a>
            <a id="butnReturn" class="btn btn-primary center-block"><span class="glyphicon glyphicon-chevron-left"></span>返回</a>
        </div>
    </div>
    <div class="grid_list_1_c" style="margin-top: 5px;">
        <table id="gridList1"></table>
        <div id="gridPager1"></div>
    </div>
    <div id="print1" class="print">
        <div class="page break_page">
            <h1>{title}</h1>
            <div class="top">
                单位：<div class="unit">{unit}</div>
                <div class="right_page">第{page}页，共{maxPage}页</div>
            </div>
            <div class="table_c">
                {table}
            </div>
            <footer>
                <span>打印人员：<label>{user}</label></span> <span>打印日期：<label>{date}</label><label>{time}</label></span>
            </footer>
        </div>
    </div>
</div>






